package cn.iocoder.yudao.module.reservation.service.rechargeorder;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.reservation.controller.admin.rechargeorder.vo.RechargeOrderPageReqVO;
import cn.iocoder.yudao.module.reservation.controller.admin.rechargeorder.vo.RechargeOrderSaveReqVO;
import cn.iocoder.yudao.module.reservation.dal.dataobject.rechargeorder.RechargeOrderDO;
import cn.iocoder.yudao.module.reservation.dal.mysql.rechargeorder.RechargeOrderMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants.RECHARGE_ORDER_NOT_EXISTS;

/**
 * 充值订单 Service 实现类
 *
 * @author yy
 */
@Service
@Validated
public class RechargeOrderServiceImpl implements RechargeOrderService {

    @Resource
    private RechargeOrderMapper rechargeOrderMapper;

    @Override
    public Long createRechargeOrder(RechargeOrderSaveReqVO createReqVO) {
        // 插入
        RechargeOrderDO rechargeOrder = BeanUtils.toBean(createReqVO, RechargeOrderDO.class);
        rechargeOrderMapper.insert(rechargeOrder);
        // 返回
        return rechargeOrder.getId();
    }

    @Override
    public void updateRechargeOrder(RechargeOrderSaveReqVO updateReqVO) {
        // 校验存在
        validateRechargeOrderExists(updateReqVO.getId());
        // 更新
        RechargeOrderDO updateObj = BeanUtils.toBean(updateReqVO, RechargeOrderDO.class);
        rechargeOrderMapper.updateById(updateObj);
    }

    @Override
    public void deleteRechargeOrder(Long id) {
        // 校验存在
        validateRechargeOrderExists(id);
        // 删除
        rechargeOrderMapper.deleteById(id);
    }

    private void validateRechargeOrderExists(Long id) {
        if (rechargeOrderMapper.selectById(id) == null) {
            throw exception(RECHARGE_ORDER_NOT_EXISTS);
        }
    }

    @Override
    public RechargeOrderDO getRechargeOrder(Long id) {
        return rechargeOrderMapper.selectById(id);
    }

    @Override
    public PageResult<RechargeOrderDO> getRechargeOrderPage(RechargeOrderPageReqVO pageReqVO) {
        return rechargeOrderMapper.selectPage(pageReqVO);
    }

}